home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14449 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  45 lines

  1. Path: mother.usf.edu!news
  2. From: gohel@csee.usf.edu (Himanshu Gohel)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: #include "" for large programs.
  5. Followup-To: comp.lang.c
  6. Date: 14 Apr 1996 21:45:54 GMT
  7. Organization: Geometric Modeling and Computer Graphics Group, USF Tampa, FL.
  8. Message-ID: <4krrmi$7ef@mother.usf.edu>
  9. References: <Pine.SUN.3.92.960411195730.24973A-100000@suntan>
  10. Reply-To: gohel@csee.usf.edu
  11. NNTP-Posting-Host: pixel.csee.usf.edu
  12.  
  13. In article <Pine.SUN.3.92.960411195730.24973A-100000@suntan>, "Carlos Diaz (CS)" <cdiaz@eng.usf.edu> writes:
  14.  
  15. > All works well when I compile the programs, except when  part2.h has
  16. >#define constants (I've been advised by my professor to use #define for
  17. >constants over 'const type var_name'). My compiler returns a fatal error
  18. >reporting that the symbol in the #define is not defined.
  19. > For example if #define EQUAL 0 is part of part2.h, I'm told that the
  20. >symbol EQUAL is not defined. But if I put the #define inside main.c, I'm
  21. >told that I've defined EQUAL  TWICE, once in part2.h and again within
  22. >main.
  23.  
  24. The way to avoid multiple definitions (and similarly multiple inclusions
  25. of .h files) is to do the following:
  26.  
  27. #ifndef __MYHEADERFILE__
  28. #define __MYHEADERFILE__
  29.  ...all your header file stuff here...
  30. #endif
  31.  
  32. Now you can include this .h file as many times as you want and you
  33. won't have problems with multiple defintions because of the #ifndef.
  34.  
  35. Also, there is a place for the const keyword, but in C that cannot
  36. be put in a .h file.  It has to go in a C file.
  37.  
  38. -- 
  39. Himanshu Gohel, gohel@csee.usf.edu         | You only have to do very few
  40. WEB URL: http://www.csee.usf.edu/~gohel/     | things right in your life as
  41. Geometric Modeling & Graphics Research Group | long as you don't do too many
  42. U of South Florida, Tampa, FL. USA.         | things wrong - Warren Buffett
  43.  
  44.  
  45.